#!/bin/sh

#
# Reading lines of numbers from file or pipe
# and successively do operation specified as second
# argument.
#
#   Copyright (C) 2009 Ultratronik Entwicklungs GmbH
#     - Author: Sören Wellhöfer <soeren.wellhoefer@gmx.net>.
#

usage() {
    echo "$0 PIPE [OPERATOR]"
}

if test "$1" == "-h"; then
    usage
    exit
fi

PIPE=$1

if test -z "$PIPE"; then
    echo "No pipe or file name given"
    usage
    exit
fi

if test -z "$2"; then
    OP="+"
else
    OP="$2"
fi

CUR=1

while read line; do
    CUR=$(expr $CUR $OP $line)
    echo $CUR
done < $PIPE